package jsu.blogger.util;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
public class StringListTypeHandler extends BaseTypeHandler<List<String>> {
    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, List<String> parameter, JdbcType jdbcType) throws SQLException {
        ps.setString(i, String.join(",", parameter)); // 将 List 转换为字符串
    }

    @Override
    public List<String> getNullableResult(ResultSet rs, String columnName) throws SQLException {
        String tags = rs.getString(columnName);
        return tags != null ? Arrays.asList(tags.split(",")) : null; // 将字符串转换为 List
    }

    @Override
    public List<String> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        String tags = rs.getString(columnIndex);
        return tags != null ? Arrays.asList(tags.split(",")) : null; // 将字符串转换为 List
    }

    @Override
    public List<String> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        String tags = cs.getString(columnIndex);
        return tags != null ? Arrays.asList(tags.split(",")) : null; // 将字符串转换为 List
    }
}

